package com.gzhryc.shared_device.cdcz.core.protocol.v2;

import com.gzhryc.common.IDTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.utils.JsonTools;
import com.gzhryc.shared_device.cdcz.core.ICDCZNetworkService;
import com.gzhryc.shared_device.cdcz.core.models.CDCZChargeCommand;
import com.gzhryc.shared_device.cdcz.core.models.CDCZExecuteResult;
import com.gzhryc.shared_device.cdcz.core.protocol.v1.V1_CDCZConstants;
import com.gzhryc.shared_device.cdcz.core.protocol.v2.models.*;
import com.gzhryc.shared_device.iot.core.IOTClient;

public class V2_CDCZNetworkService implements ICDCZNetworkService {

    static Logger log = Logger.getLogger(V2_CDCZNetworkService.class);

    private final IOTClient iotClient;

    public V2_CDCZNetworkService(IOTClient iotClient){
        this.iotClient = iotClient;
    }

    @Override
    public CDCZExecuteResult charge(CDCZChargeCommand chargeCommand) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            V2CDCZChargeRequest iotReq = new V2CDCZChargeRequest();
            //协议
            iotReq.setDEVICE(chargeCommand.getDeviceSn());
            iotReq.setSEQ(chargeCommand.getMessageId());
            iotReq.setPORT(chargeCommand.getSlotNum());
            iotReq.setTIME(chargeCommand.getChargeTimes() * 60);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setDeviceSn(chargeCommand.getDeviceSn());
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(chargeCommand.getMessageId());
            cloudMessage.setTopic("/" + chargeCommand.getDeviceSn() + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult closeCharge(String deviceSn, Integer slotNum) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = V1_CDCZConstants.getMessageId();

            V2CDCZCloseChargeRequest iotReq = new V2CDCZCloseChargeRequest();
            //协议
            iotReq.setDEVICE(deviceSn);
            iotReq.setSEQ(msgId);
            iotReq.setPORT(slotNum);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult searchDevice(String deviceSn) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZSearchRequest iotReq = new V2CDCZSearchRequest();
            //协议
            iotReq.setDEVICE(deviceSn);
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult restart(String deviceSn) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZRestartRequest iotReq = new V2CDCZRestartRequest();
            //协议
            iotReq.setDEVICE(deviceSn);
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult findSlaveDevice(String mainDevice,Integer bindIndex) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZFindSlaveDeviceRequest iotReq = new V2CDCZFindSlaveDeviceRequest();
            //协议
            iotReq.setDEVICE(mainDevice);
            iotReq.setSLAVENUM(bindIndex);
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(mainDevice);
            cloudMessage.setTopic("/" + mainDevice + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult bindSlaveDevice(String mainDevice,Integer bindIndex,String deviceSn) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZBindSlaveDeviceRequest iotReq = new V2CDCZBindSlaveDeviceRequest();
            String slaveName = deviceSn;
            if(!slaveName.startsWith("XH_")){
                slaveName = "XH_" + slaveName;
            }
            //协议
            iotReq.setDEVICE(mainDevice);
            iotReq.setSLAVENAME(slaveName);
            iotReq.setSLAVENUM(bindIndex);
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(mainDevice);
            cloudMessage.setTopic("/" + mainDevice + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult removeSlaveDevice(String mainDevice) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZRemoveSlaveDeviceRequest iotReq = new V2CDCZRemoveSlaveDeviceRequest();
            //协议
            iotReq.setDEVICE(mainDevice);
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(mainDevice);
            cloudMessage.setTopic("/" + mainDevice + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public CDCZExecuteResult changeMQTTConnect(String deviceSn,String type,String ip,String port,String userName,String userPwd){
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V2CDCZDeviceLoginResponse iotReq = new V2CDCZDeviceLoginResponse();
            iotReq.setDEVICE(deviceSn);
            iotReq.setSERVERTYPE(type);
            iotReq.setADDRESS(ip);
            iotReq.setPORT(port);
            iotReq.setMQTTUSER(userName);
            iotReq.setMQTTPASS(userPwd);
            iotReq.setREVISE(1);        //0 不切换，1 切换服务器，设备会自动重启
            iotReq.setSEQ(msgId);

            V2_CDCZ_IOTCloudMessage cloudMessage = new V2_CDCZ_IOTCloudMessage(JsonTools.toJson(iotReq));
            cloudMessage.setCmd(iotReq.getCMD().toString());
            cloudMessage.setMessageId(msgId);
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult upgradeDownload(String deviceSn, String packagePath, String packageCRC) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        return result;
    }

    @Override
    public CDCZExecuteResult upgrade(String deviceSn, String packagePath, String packageCRC) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        return result;
    }
}
